package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Lorenzo Nuvoletta
	 */
	public class Main extends Sprite 
	{
		private var sprite:Sprite;
		private var BG:bg;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			sprite = new Sprite();
			addChild(sprite);
			BG = new bg();
			test2();
			addEventListener(Event.ENTER_FRAME, move);
		}
		
		private function test1():void {
			sprite.addChild(BG);
			BG.cacheAsBitmap = true;
			var rect:Rectangle = BG.getBounds(null);
			BG.x -= rect.x;
			BG.y -= rect.y;
		}
		
		private function test2():void {		
			var rect:Rectangle = BG.getBounds(null);
			var bitmapdata:BitmapData = new BitmapData(rect.width, rect.height);
			var matrix:Matrix = new Matrix();
			
			matrix.translate( -rect.x, -rect.y);
			bitmapdata.draw(BG, matrix);
			var bitmap:Bitmap = new Bitmap(bitmapdata);
			sprite.addChild(bitmap);
		}
		
		private function move(e:Event):void {
			sprite.x--;
			sprite.y--;
		}
		
	}
	
}